package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.enums.DictEnum;
import com.atguigu.yygh.hosp.repository.HospitalRepository;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author:hx
 * @DateTime:2022/11/7 18:22
 * @Description:
 */
@Service
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private HospitalRepository hospitalRepository;
    @Autowired
    private DictFeignClient dictFeignClient;


    @Override
    public void saveHospital(Map<String, Object> paramMap) {
        Hospital hospital = JSONObject.parseObject(JSONObject.toJSONString(paramMap), Hospital.class);
        //根据hoscode在mongo中进行查询
        Query query = new Query();
        query.addCriteria(Criteria.where("hoscode").is(hospital.getHoscode()));
        Hospital mongoHospital = mongoTemplate.findOne(query, Hospital.class);
        //判断添加还是修改
        if (mongoHospital == null){
            //添加
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setIsDeleted(0);
            hospital.setUpdateTime(new Date());
            mongoTemplate.save(hospital);
        }else {
            //修改
            hospital.setStatus(mongoHospital.getStatus());
            hospital.setCreateTime(mongoHospital.getCreateTime());
            hospital.setIsDeleted(mongoHospital.getIsDeleted());
            hospital.setUpdateTime(new Date());
            hospital.setId(mongoHospital.getId());
            mongoTemplate.save(hospital);
        }
    }

    @Override
    public Hospital show(String hoscode) {
        Query query = new Query(Criteria.where("hoscode").is(hoscode));
        Hospital hospital = mongoTemplate.findOne(query, Hospital.class);
        return hospital;
    }

    @Override
    public Page<Hospital> findPage(Integer pageNum, Integer pageSize, HospitalQueryVo hospitalQueryVo) {
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        hospital.setIsDeleted(0);
        Example<Hospital> example = Example.of(hospital,
                ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true));
        Pageable pageable = PageRequest.of(pageNum - 1,pageSize);
        Page<Hospital> page = hospitalRepository.findAll(example, pageable);

        page.getContent().forEach(this::packHospital);

        return page;
    }

    private Hospital packHospital(Hospital hospital){
        String hostype = dictFeignClient.getName( DictEnum.HOSTYPE.getDictCode(),Long.parseLong(hospital.getHostype()));
        String province = dictFeignClient.getName(Long.parseLong(hospital.getProvinceCode()));
        String city = dictFeignClient.getName(Long.parseLong(hospital.getCityCode()));
        String district = dictFeignClient.getName(Long.parseLong(hospital.getDistrictCode()));
        hospital.getParam().put("hostype",hostype);
        hospital.getParam().put("fullAddress",province + city + district + hospital.getAddress());
        return hospital;
    }

    @Override
    public void updateStatus(String id, Integer status) {
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    @Override
    public Map<String,Object> getHospById(String id) {
        Map<String,Object> map = new HashMap<>();
        Hospital hospital = hospitalRepository.findById(id).get();
        this.packHospital(hospital);
        map.put("hospital",hospital);
        map.put("bookingRule",hospital.getBookingRule());
        return map;
    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospital = new Hospital();
        hospital.setHoscode(hoscode);
        Example<Hospital> example = Example.of(hospital);
        return hospitalRepository.findOne(example).get();
    }

    @Override
    public List<Hospital> getByHosname(String hosname) {
        List<Hospital> hosnameList = new ArrayList<>();
//        Map<String,String> map = new HashMap<>();
        Hospital hospital = new Hospital();
        hospital.setHosname(hosname);
        Example<Hospital> example = Example.of(hospital,ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING));
        List<Hospital> hospitalList = hospitalRepository.findAll(example);
        /*for (Hospital hospital1 : hospitalList) {
            map.put("hosname",hospital1.getHosname());
        }
        hosnameList.add(map);*/
        return hospitalList;
    }

    @Override
    public Map<String, Object> showByHoscode(String hoscode) {
        Map<String,Object> map = new HashMap<>();
        Hospital hospital = new Hospital();
        hospital.setHoscode(hoscode);
        Example<Hospital> example = Example.of(hospital);
        Hospital hospital1 = hospitalRepository.findOne(example).get();
        this.packHospital(hospital1);
        map.put("hospital",hospital1);
        map.put("bookingRule",hospital1.getBookingRule());
        return map;
    }
}
